package cn.tl.org.service.impl;

import cn.tl.base.service.impl.BaseServiceImpl;
import cn.tl.org.domain.Employee;
import cn.tl.org.domain.Shop;
import cn.tl.org.mapper.EmployeeMapper;
import cn.tl.org.mapper.ShopMapper;
import cn.tl.org.service.IShopService;
import cn.tl.org.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.internet.MimeMessage;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        employeeMapper.save(shop.getAdmin());
        shopMapper.save(shop);
        shop.getAdmin().setShop_id(shop.getId());
        employeeMapper.update(shop.getAdmin());
    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        shopMapper.patchDel(ids);
    }

    @Override
    public AjaxResult find(Long id) {
        shopMapper.amendShop(1,id);
        Employee shopById = employeeMapper.findShopById(id);
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1056588907@qq.com");
            helper.setTo(shopById.getEmail());
            Long id1 = shopById.getId();
            helper.setSubject("点击激活");
            helper.setText("<a href='http://127.0.0.1:8080/employee/upt/"+id1+"'><h1>点击激活</h1>\n" +
                    "    </a>" ,true);
            javaMailSender.send(mimeMessage);
            return AjaxResult.me();


        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }
}
